﻿#region Using
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion Using

namespace LloydGame
{
    public class SimpleImageProcessor : ImageProcessor
    {
        #region Class Fields
        protected RenderTarget2D m_renderTarget;
        #endregion Class Fields

        // constructor
        public SimpleImageProcessor(Game p_game, int p_imageWidth, int p_imageHeight)
            : base(p_game, p_imageWidth, p_imageHeight)
        {
            m_renderTarget = new RenderTarget2D(
                p_game.GraphicsDevice,
                p_imageWidth,
                p_imageHeight,
                1,
                p_game.GraphicsDevice.DisplayMode.Format);

            m_depthStencil = new DepthStencilBuffer(
                m_renderTarget.GraphicsDevice,
                m_renderTarget.Width,
                m_renderTarget.Height,
                m_renderTarget.GraphicsDevice.DepthStencilBuffer.Format,
                m_renderTarget.MultiSampleType,
                m_renderTarget.MultiSampleQuality);

            m_effect = Game.Content.Load<Effect>("Effects\\SimpleImageProcessing");
        }

        // process the image
        public Texture2D process(Texture2D p_image, String p_type)
        {
            m_effect.CurrentTechnique = m_effect.Techniques[p_type];

            m_effect.Parameters["dx"].SetValue(1.0f / p_image.Width);
            m_effect.Parameters["dy"].SetValue(1.0f / p_image.Height);

            m_effect.Parameters["image"].SetValue(p_image);

            Game.GraphicsDevice.SetRenderTarget(0, m_renderTarget);

            processPass(0);

            Game.GraphicsDevice.SetRenderTarget(0, null);

            return m_renderTarget.GetTexture();
        }
    }
}
